package br.com.musclebook.entity;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import br.com.musclebook.enums.ExerciseCategory;

@Entity
@Table(name = "EXECUTION_SET")
public class ExecutionSet implements Serializable, Comparable<ExecutionSet> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "ORDER_NUMBER")
    private Integer orderNumber;

    private Integer series;

    private Integer repetitions;

    private BigDecimal weight;

    @ManyToOne
    @JoinColumn(name = "EXERCISE_EXECUTION_SET_ID")
    private ExerciseExecutionSet exerciseExecutionSet;

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public Integer getOrderNumber() {
	return orderNumber;
    }

    public void setOrderNumber(Integer orderNumber) {
	this.orderNumber = orderNumber;
    }

    public Integer getSeries() {
	return series;
    }

    public void setSeries(Integer series) {
	this.series = series;
    }

    public Integer getRepetitions() {
	return repetitions;
    }

    public void setRepetitions(Integer repetitions) {
	this.repetitions = repetitions;
    }

    public BigDecimal getWeight() {
	return weight;
    }

    public void setWeight(BigDecimal weight) {
	weight.setScale(2, BigDecimal.ROUND_HALF_EVEN);
	this.weight = weight;
    }

    public ExerciseExecutionSet getExerciseExecutionSet() {
	return exerciseExecutionSet;
    }

    public void setExerciseExecutionSet(ExerciseExecutionSet exerciseExecutionSet) {
	this.exerciseExecutionSet = exerciseExecutionSet;
    }

    @Override
    public int compareTo(ExecutionSet o) {
	String thisRoutineName = this.getExerciseExecutionSet().getRoutineDayTraining().getRoutineDay().getRoutine().getName();
	String thatRoutineName = o.getExerciseExecutionSet().getRoutineDayTraining().getRoutineDay().getRoutine().getName();
	
	Integer thisRoutineDay = this.getExerciseExecutionSet().getRoutineDayTraining().getRoutineDay().getDayOfWeek();
	Integer thatRoutineDay = o.getExerciseExecutionSet().getRoutineDayTraining().getRoutineDay().getDayOfWeek();
	
	ExerciseCategory thisExercCategory = this.getExerciseExecutionSet().getRoutineDayTraining().getExerciseCategory();
	ExerciseCategory thatExercCategory = o.getExerciseExecutionSet().getRoutineDayTraining().getExerciseCategory();
	
	Integer thisExerc = this.getExerciseExecutionSet().getOrderNumber();
	Integer thatExerc = o.getExerciseExecutionSet().getOrderNumber();
	
	if(thisRoutineName.equals(thatRoutineName)) { // compare name
	    if(thisRoutineDay.equals(thatRoutineDay)) { // compare day
		if(thisExercCategory.equals(thatExercCategory)) { // compare category
		    if(thisExerc.equals(thatExerc)) { // compare exercise
			// compare order number
			return this.orderNumber.compareTo(o.orderNumber);
		    } else {
			return thisExerc.compareTo(thatExerc);
		    }
		} else {
		    return thisExercCategory.compareTo(thatExercCategory);
		}
	    } else {
		return thisRoutineDay.compareTo(thatRoutineDay);
	    }
	} else  {
	    return thisRoutineName.compareTo(thatRoutineName);
	}
    }
}
